﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using wne.Context;
using wne.core.Interface;
using wne.models;

namespace wne.core
{
    public class SenderFacadeSql : SenderFacade
    {
        public override void AddDocumentToStorage(int docId, string userName)
        {
            using (var context = new tpksEntities())
            {
                var userId =
                    context.User.First(x => x.Login.Equals(userName, StringComparison.InvariantCultureIgnoreCase)).User_ID;
                var shDoc = new ShedulerDocs
                {
                    Doc_ID = docId,
                    Status = (short)DocStatus.Created,
                    OwnerId = userId,
                    UniqueId = Guid.NewGuid(),
                    ShedulerFiles = new Collection<ShedulerFiles>()
                };
                context.ShedulerDocs.Add(shDoc);
                var currDoc = context.Doc.First(x => x.Doc_ID == docId);
                shDoc.ShedulerFiles = 
                    currDoc.Doc_Data
                        .Where(x => x.Step_Field.Field_Type == (int)FieldTypeEnum.File).ToList()
                        .Select(x =>new ShedulerFiles
                        {
                            UniqueAttachmentId = Guid.NewGuid(),
                            AttachmentStatus = 0,
                            Name = x.Value
                        })
                        .ToList();
            }
        }

        public override void GiveDocumentFromStorage(string guid, string password,string userName)
        {
            var client = new GlobalWcf.GlobalWcfClient();
            var passHash = MD5.Create().ComputeHash(Convert.FromBase64String(password));
            var docFromStorage = client.GetDocument(Guid.Parse(guid),passHash);
            var xmlSerialize = new XmlSerializer(typeof(Fulldoc));
            var reader = new StringReader(docFromStorage.Document);
            var documentFull = xmlSerialize.Deserialize(reader);
            int newId = (new DocumentFacadeSql()).AddOutDoc((Fulldoc)documentFull);
            using (var context = new tpksEntities())
            {
                var userId =
                    context.User.First(x => x.Login.Equals(userName, StringComparison.InvariantCultureIgnoreCase)).User_ID;
                var shDoc = new ShedulerDocs
                {
                    Doc_ID = newId,
                    UniqueId = docFromStorage.Id,
                    Password = passHash,
                    IsForSend = 0,
                    OwnerId = userId,
                    Status = (short) DocStatus.Created,
                    ShedulerFiles = docFromStorage.AttachmentsGuid.Select(x => new ShedulerFiles
                    {
                        UniqueAttachmentId = x
                    }).ToList()
                };
                context.ShedulerDocs.Add(shDoc);
            }
        }
    }
    public enum DocStatus
    {
        Created, InProcess, Success
    }
}
